package common.util {

	import common.cache.CustomerCache;
	import common.cache.DcChinaAreaCache;
	import common.cache.DepCache;
	import common.cache.MenuCache;
	import common.cache.StaffCache;
	import common.cache.SystemCache;
	import common.cache.TradeCache;
	import common.data.admin.LoginService;
	import common.data.customerservice.CustomerService;
	import common.remote.RemoteService;
	
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.ResultEvent;
	
	import spark.components.Application;
	
	import ui.maps.MapGroup;
	import ui.message.MessageBoxUtil;
	import ui.tree.BaseTree;
	
	
	public class LoginUtil {
		private static var _callback:Function;
		private static var _scope:Application;
		private static var timer:Timer;
		
		//调试登录
		public static function debugLogin(callback:Function,scope:Application ,userName:String = 'super_admin'):void{
			_callback = callback;
			_scope = scope;
			
			//远程服务
			var service:LoginService = new LoginService();
			service.debugLoginCallBack = function(e:ResultEvent):void{_callback.call(_scope)};
			service.debugLogin(userName);
		}
		
		//调试登录
		public static function locateLogin(userName:String,password:String,callback:Function,scope:Application):void{
			_callback = callback;
			_scope = scope;
			
			//远程服务
			var service:LoginService = new LoginService();
			service.locateLoginCallBack = function(e:ResultEvent):void{_callback.call(_scope,e)};
			service.locateLogin(userName,password);
		}
		
		//登录获取信息
		public static function setLoginInfo(callback:Function,scope:Application):void{
			_callback = callback;
			_scope = scope;
			
			var service:LoginService = new LoginService();
			service.getLoginInfoCallBack = returnLoginInfo;
			service.getLoginInfo();
		}
		
		private static function returnLoginInfo(e:ResultEvent):void{
			if(!e.result){//如果类型没有自动转换，请最上面引入该类型
				navigateToURL(new URLRequest(SystemCache.loginFolder+"login.jsp"),"_self");
				return;
			}
			try{
				var obj:Object = DataUtil.decompress(e.result as ByteArray);
				
				SystemCache.user = obj.user;
				SystemCache.googleApiKey = obj.gmapKey;
				SystemCache.mapabcKey = obj.mapabcKey;
				SystemCache.company = obj.company;
				SystemCache.menus = obj.menus;
				SystemCache.hasSendSmsMenu = obj.hasSendSmsMenu;
				
				SystemCache.setCompanyInfo();
				SystemCache.needToAlarm = obj.needToAlarm;
				SystemCache.isHebWq = obj.isHebWq;
				SystemCache.outerParamMap = obj.outerParamMap||{};
				
				MenuCache.init(obj.menus);
				
				DepCache.depList = obj.deps;
				DepCache.toTreeData();
				
				StaffCache.staffList = obj.staffs;
				StaffCache.toTreeData();
				
			}catch(e:Error){
				trace(e);
				MessageBoxUtil.warnMessage('系统初始化异常！');
			}
			SystemCache.mapGroup = new MapGroup();
			
			if(_callback != null){
				_callback.call(_scope);
			}
		}
		
		public static function refreshDeps(tree:BaseTree):void{
			var service:LoginService = new LoginService();
			service.refreshDepsCallBack = function (e:ResultEvent):void{
				var obj:Object = DataUtil.decompress(e.result as ByteArray);
				DepCache.depList = obj as ArrayCollection;
				DepCache.toTreeData();
				tree.resetDataProvider();
			};
			service.refreshDeps();
		}
		
		public static function refreshDepsStaffs(tree:BaseTree):void{
			var service:LoginService = new LoginService();
			service.refreshDepsStaffsCallBack = function (e:ResultEvent):void{
				var obj:Object = DataUtil.decompress(e.result as ByteArray);
				DepCache.depList = obj[0] as ArrayCollection;
				DepCache.toTreeData();
				
				StaffCache.staffList = obj[1] as ArrayCollection;
				StaffCache.toTreeData();
				
				tree.resetDataProvider();
			};
			service.refreshDepsStaffs();
		}
		
		public static function refreshClientInfo(tree:BaseTree):void{
			CustomerCache.getClientDataFormService(function():void{
				tree.resetDataProvider();
			},tree);
		}
		
	}
}